package cn.jiedh.app.meiweidk.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.jiedh.app.meiweidk.Utils.UI.LoadDialog;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.network.ResponseFailure;
import cn.jiedh.app.meiweidk.network.ResponseSuccess;

import org.xutils.ex.HttpException;

import java.util.Map;

import butterknife.ButterKnife;


/**
 * @name：
 * @author： 司马林
 * @phone： 18423134135
 * @createTime： 2017/10/19
 * @modifyTime： 2017/10/19
 * @explain：
 */
public abstract class BaseFragment extends Fragment {
    protected abstract int getLayoutView();

    protected abstract void initView(View view);

    protected abstract void initData();

    private LoadDialog loadDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutView(), container, false);
        ButterKnife.bind(this, view);
        initView(view);
        initData();
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void request(String url, Map<String, Object> map, boolean isEncrypt, ResponseSuccess success, ResponseFailure failure) {

        if (getActivity() instanceof BaseActivity) {
            BaseActivity ba = (BaseActivity) getActivity();
            ba.request(ba, url, map, isEncrypt, success, failure);

        }
    }

    public void request(String url, Map<String, Object> map, boolean isEncrypt, ResponseSuccess success) {

        if (getActivity() instanceof BaseActivity) {
            BaseActivity ba = (BaseActivity) getActivity();
            ba.request(ba, url, map, isEncrypt, success);

        }
    }




    public void defaultRequestError(HttpException error, String msg) {
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).dismissLoadingDialog();
        }
        if (!TextUtils.isEmpty(msg)) {
            T.showShortToast(msg);
        }
    }

    public void showLoadingDialog() {
        if (loadDialog != null && loadDialog.isShowing()) {
            loadDialog.dismiss();
        }
        if (loadDialog == null) {
            loadDialog = new LoadDialog(getActivity());
        }
        loadDialog.show();

	}

	public void dismissLoadingDialog() {
		if (loadDialog!=null && loadDialog.isShowing()) {
			loadDialog.dismiss();
		}
	}
}
